﻿using System.Collections.Generic;
using System.Net.Sockets;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Log;

namespace ConAppCore.Test
{
    internal class TestSpinThread : ISpinThreadAction
    {

        /// <summary>
        /// 执行动作
        /// </summary>
        void ISpinThreadAction.Excute()
        {
            Loger.Info($"{this.GetHashCode()}_Excute");
        }

        private static SpinThread _spinThread = null;
        public static void Test()
        {
            int count = 3;
            _spinThread = new SpinThread(count);
            for (int i = 0; i < count; i++)
            {
                _spinThread.AddAction(new TestSpinThread());
            }
            _spinThread.Start();
        }


        private static void TestSocketReadAndWrite(IEnumerable<Socket> sockets)
        {
            byte[] buffer = new byte[1024];
            foreach (var socket in sockets)
            {
                if (socket.Poll(0, SelectMode.SelectWrite))
                {
                    socket.Send(buffer);
                }
            }


            int rcvLen;
            foreach (var socket in sockets)
            {
                if (socket.Poll(0, SelectMode.SelectRead))
                {
                    rcvLen = socket.Receive(buffer);
                    //...
                }
            }
        }
    }
}
